package cn.xc.org.service.impl;


import cn.xc.basic.service.impl.BaseServiceImpl;
import cn.xc.basic.util.BaiduAiUtils;
import cn.xc.org.domain.Employee;
import cn.xc.org.domain.Shop;
import cn.xc.org.domain.ShopAuditLog;
import cn.xc.org.domain.ShopDot;
import cn.xc.org.exception.BusinessException;
import cn.xc.org.mapper.EmployeeMapper;
import cn.xc.org.mapper.ShopAuditLogMapper;
import cn.xc.org.mapper.ShopMapper;
import cn.xc.org.service.IShopService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;


    @Transactional
    @Override
    public void add(Shop shop) {
        shop.setRegisterTime(new Date());
        shopMapper.save(shop);
    }

    @Override
    public void settlement(Shop shop) {
        String name = shop.getName();
        String address = shop.getAddress();
        String tel = shop.getTel();

        if(StringUtils.isEmpty(address)||StringUtils.isEmpty(name)||StringUtils.isEmpty(tel)){
            throw  new BusinessException("数据不能为空");
        }
        Shop dbShop = shopMapper.loadByNameAndAddress(shop);
        if(dbShop!=null){
            throw new BusinessException("该店铺已入驻,请直接登录");
        }

        //4.智能審核
        if (!BaiduAiUtils.textCensor(shop.getName())){
            throw new BusinessException("請輸入正確的店鋪名稱!!");
        }
        String img = "http://123.207.27.208:22122" + shop.getLogo();
        if (BaiduAiUtils.imgCensor(img)){
            throw new BusinessException("請傳入正確的店鋪logo!!");
        }

        //二:保存數據
        //保存
        Employee admin = shop.getAdmin();
        employeeMapper.save(admin);

        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);

        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException { //shopId,note
        //改變狀態:1 - 2
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(2);
        shopMapper.update(shop);

        //添加審核日誌
        shopAuditLog.setState(2);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");

        messageHelper.setFrom("3206009761@qq.com");
        messageHelper.setSubject("啦啦啦");
        messageHelper.setText("<h1>店鋪烏拉</h1><br><a href='" +
                "http://localhost:8080/shop/active/?id="+
                shop.getId()+"'>點擊鏈接激活店鋪</a>",true);
        messageHelper.setTo("3206009761@qq.com");
        javaMailSender.send(mimeMessage);

    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
        //改變狀態: 1 - 4
        Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
        shop.setState(4);
        shopMapper.update(shop);

        //添加審核日誌
        shopAuditLog.setState(4);
        shopAuditLog.setAuditId(325L);
        shopAuditLogMapper.save(shopAuditLog);

        //發送郵件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
        //發件人
        messageHelper.setFrom("3206009761@qq.com");
        //主體
        messageHelper.setSubject("哦豁，豁");
        //正文
        messageHelper.setText("<h1>店鋪烏拉</h1><br><a href='http://localhost:8081/#/register'>點擊鏈接重新激活</a>",true);
        //收件人
        messageHelper.setTo("3206009761@qq.com");
        //確認發送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDot> echarts() {
        return shopMapper.echarts();
    }
}
